package com.yunji.storage.pattern;

import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.enums.common.CacheCodeExtend;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.RepairSelectQueryBO;
import com.yunji.storage.domain.RepairTicketDO;
import com.yunji.storage.domain.StockDO;
import com.yunji.storage.enums.RepairStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.storage.pattern
 * @Title: DiscardInteract
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-27 09:17:11      V1.0        HuaAo       新建类
 */
@Component
public class RepairInteract extends AbstractStorage {


    @Override
    protected void addStorage(StockDO stock) {
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        // 坏返单是操作不可用库存
        logs.info("【坏返库存处理】新增库存:{}", actualQuantity);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setDisableStock(actualQuantity);
        update.setAllStock(actualQuantity);

        Assert.isTrue(stockService.updateStock(update), "入库失败");
    }

    @Override
    protected void subtractStock(StockDO stock) {
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        logs.info("【坏返库存处理】扣减库存:{}", actualQuantity);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setDisableStock(actualQuantity.negate());
        update.setAllStock(actualQuantity.negate());

        Assert.isTrue(stockService.updateStock(update), "出库失败");
    }

    @Override
    protected void backStock(StockDO stock) {
        logs.info("【坏返库存处理】返还库存:{}", stock);
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setUsableStock(actualQuantity);
        update.setAllStock(actualQuantity);

        Assert.isTrue(stockService.updateStock(update), "出库失败");
    }

    @Override
    protected void createBackStock(StockDO stock) {
        logs.info("【坏返库存处理】新增返还库存:{}", stock);
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        StockDO create = new StockDO();
        create.setBelongId(stock.getBelongId());
        create.setProductId(stock.getProductId());
        create.setUsableStock(actualQuantity);
        create.setAllStock(actualQuantity);

        Assert.isTrue(stockService.create(create), "出库失败");
    }


    @Override
    public void updateTicket(Long businessId) {
        logs.info("【坏返状态更改】坏返ID:{}", businessId);

        RepairTicketDO update = new RepairTicketDO();
        update.setRepairId(businessId);
        update.setRepairStatus(RepairStatus.FINISH.getCode());
        Assert.isTrue(repairTicketService.update(update), "修改失败");
    }

    @Override
    public void checkTicket(String businessCode) {
        logs.info("【检验坏返单】坏返单号:{}", businessCode);

        RepairTicketDO vo = repairTicketService.select(new RepairSelectQueryBO(businessCode));
        Assert.isTrue(BeanUtils.isNotNull(vo), ErrorEnum.BUSINESS_812016.getText());

        redisCache.set(CacheCodeExtend.PARAM_001002.getInfo(), vo.getRepairId());
    }
}